<template>
  <div class="login-container">
    <div class="system-login" style="height:100%;width:100%;display:block;position:relative;height:100%;">
      <div class="background" style="position: absolute; top: 0; overflow: hidden; height: 100%; width: 100%; ">
        <img src="@/assets/login/background_down.jpg" style="height: 100%; width: 100%; top: 0" alt="">
      </div>
      <el-form ref="loginForm" :model="loginForm" class="login-form" autocomplete="on" label-position="center" style="height: 100%">
        <div class="background" style="  position: absolute; top: 40px; left: 40px; overflow: hidden; height: calc(100% - 80px); width: calc(100% - 80px); ">
          <div style="height: calc(100% + 80px); width: calc(100% + 80px)">
            <img src="@/assets/login/background_up.png" style="      padding: 0; margin: 0; margin-left: -40px; margin-top: -40px; width: 100%;  height: 100%;  display: block;" alt="">
          </div>
        </div>
        <!--<a href="/"class="logo-version"><img src="{$_W['attachurl']}{$data['logo']}"class="logo"><span class="version hidden">{IMS_VERSION}</span></a>-->
        <div style="position: absolute; top: 40px; left: 40px; overflow: hidden; height: calc(100% - 80px); width: calc(100% - 80px); display: flex; ">
          <div style="height: 100%; flex: 9; position: relative">
            <a href="" style="position: absolute; left: 40px; top: 40px">
              <!-- <span style="color: white; font-size: 35px">鹿城区文明大脑数字系统</span> -->
              <img src="@/assets/login/logo.png" alt="" />
            </a>
            <div style="  position: absolute; right: 80px; bottom: 50px; text-align: right; ">
              <img src="@/assets/login/user_management_background.png" style="width: 80%" alt="">
            </div>
          </div>
          <div class="" style="    flex: 4;  background: white;  display: flex;  align-items: center; justify-content: center; ">
            <div style="width: 85%">
              <div class="title form-input">
                <img src="@/assets/login/welcome.png" alt="">
              </div>
              <div class="we7-form">
                <div class="input-group-vertical form-input">
                  <input v-model="loginForm.username" name="username" type="text" class="form-control" placeholder="输入登录账号">
                  <input v-model="loginForm.password" name="password" type="password" class="form-control password" placeholder="输入登录密码">
                </div>
                <!--<div class="checkbox form-input"><input type="checkbox"value="true"id="rember"name="rember"><label for="rember">记住用户名</label></div>-->
                <div class="login-submit text-center form-input">
                  <el-button :loading="loading" :class="'btn-primary'" @click.native.prevent="handleLogin">登录</el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
// import SocialSign from './components/SocialSignin'
import { getWordList } from '@/api/folder'

export default {
  name: 'Login',
  components: {
    // SocialSign
  },
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   // if (!validUsername(value)) {
    //   //   callback(new Error('Please enter the correct user name'))
    //   // } else {
    //   callback()
    //   // }
    // }
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 5) {
    //     callback(new Error('The password can not be less than 5 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loginForm: {
        username: '',
        password: '',
      },
      // loginRules: {
      //   username: [
      //     { required: true, trigger: "blur", validator: validateUsername },
      //   ],
      //   password: [
      //     { required: true, trigger: "blur", validator: validatePassword },
      //   ],
      // },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      disabled: true,
      isLogin: false,
      disBtn: true,
      timer: '',
      count: 0,
      otherQuery: {},
      show: false,
      html: '',
    }
  },
  watch: {
    // $route: {
    //   handler: function (route) {
    //     const query = route.query
    //     if (query) {
    //       this.redirect = query.redirect
    //       this.otherQuery = this.getOtherQuery(query)
    //     }
    //   },
    //   immediate: true,
    // },
    // isLogin: {
    //   handler: function (flag) {
    //     if (!flag) {
    //       this.disabled = true
    //     }
    //   },
    // },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    // i = 0;
    // i++;
    // if (i == 1) {
    //   location.reload();
    // }
  },
  mounted() {
    // if (this.loginForm.username === '') {
    //   this.$refs.username.focus()
    // } else if (this.loginForm.password === '') {
    //   this.$refs.password.focus()
    // }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    confirmLogin() {
      this.isLogin = true
      this.showDialog = false
      this.disabled = false
    },
    beforeLogin() {
      this.showDialog = true
      new Promise((resolve, reject) => {
        getWordList().then((response) => {
          this.html = response.data[0].html
          this.count = 5
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count < 6) {
              this.count--
            } else {
              this.disBtn = false
              this.show = true
              clearInterval(this.timer)
              this.timer = null
            }
          }, 1000)
        })
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (this.loginForm.username == '') {
        this.$message({
          message: '请填写用户名!',
          type: 'error',
        })
        return
      }
      if (this.loginForm.password == '') {
        this.$message({
          message: '请填写密码!',
          type: 'error',
        })
        return
      }
      // if (this.isLogin == false) {
      //   this.$message({
      //     message: '登录前请先同意须知!',
      //     type: 'error',
      //   })
      //   return
      // }
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then((response) => {
              this.$router.push('/')
              this.loading = false
              this.$message({
                message: '登录成功',
                type: 'success',
              })
            })
            .catch(() => {
              // console.log("我catch了");
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        // if (cur !== 'redirect') {
        // }
        return acc
      }, {})
    },
  },
}
</script>

<style>
.login-container {
  width: 100%;
  height: 100%;
  /* background-image: url("../../assets/loginBg.png"); */
  background-size: 100% 100%;
  background-repeat: no-repeat;
  z-index: 0;
  /* padding: 5% 10% 5% 10%; */
  display: flex;
  align-items: center;
}

.form-input {
  margin-bottom: 50px;
  text-align: center;
}
.btn-primary {
  width: 100%;
  color: #fff;
  border: 1px solid transparent;
  border-radius: 4px;
  font-size: 16px;
  margin-bottom: 15px;
  background-image: repeating-linear-gradient(90deg, #175fc9, #2e76e2);
  height: 50px!;
}
.form-control {
  display: block;
  width: 100%;
  height: 34px;
  padding: 6px 12px;
  font-size: 14px;
  border-radius: 4px;
  margin-bottom: 20px;
  background: #f7f7f7;
  border: 0;
  text-align: center;
  height: 50px;
}
</style>
